package pl.vojna.ant_alg;

import java.util.Arrays;

public class Solution {
	private final int[] array;
	private Integer hash;
	private Integer cost;
	private final Problem problem;

	public Solution(Problem problem, int[] array) {
		this.problem = problem;
		this.array = array;
	}

	public Solution(Problem problem, int[] array, Integer hash, Integer cost) {
		this.problem = problem;
		this.array = array;
	}

	public int get(int index) {
		return array[index];
	}

	@Override
	public int hashCode() {
		if (hash != null) {
			return hash;
		}
		hash = Utils.solutionHash(array);
		return hash;
	}

	/**
	 * Zamienia pozycjami dwa elementy permutacji.
	 * 
	 * @param r
	 * @param s
	 */
	public void swap(int r, int s) {
		hash = null;
		cost = null;
		int tmp = array[r];
		array[r] = array[s];
		array[s] = tmp;
	}

	public int[] getArray() {
		return array;
	}

	public int getCost() {
		if (cost != null) {
			return cost;
		}
		return cost = Utils.solutionCost(problem, array);
	}

	public void show() {
		Utils.showSolution(array);
	}

	public Solution clone_() {
		return new Solution(problem, Arrays.copyOf(array, array.length), hash,
				cost);
	}
}
